/**
 * 请求头
 */
var config = require("./config")
var app = getApp()
var header = {
  "Content-Type": "application/x-www-form-urlencoded"
  // 'Authorization': "Bearer " + wx.getStorageSync("token"),
  // 'os': 'android',
  // 'version': '1.0.0',
  // 'device_token': 'ebc9f523e570ef14',
}
/**
 * 供外部post请求调用  
 */
function post(url, params, onSuccess, onFailed) {
  request(url, params, "POST", onSuccess, onFailed);
}

/**
 * 供外部get请求调用
 */
function get(url, params, onSuccess, onFailed) {
  request(url, params, "GET", onSuccess, onFailed);
}

function del(url, params, onSuccess, onFailed) {
  request(url, params, "DELETE", onSuccess, onFailed);
}

function put(url, params, onSuccess, onFailed) {
  request(url, params, "PUT", onSuccess, onFailed);
}
/**
 * function: 封装网络请求
 * @url URL地址
 * @params 请求参数
 * @method 请求方式：GET/POST
 * @onSuccess 成功回调
 * @onFailed  失败回调
 */

function request(url, params, method, onSuccess, onFailed) {

  // wx.showLoading({
  //   title: "正在加载中...",
  // })
  wx.showLoading()
  if (app.globalData.token != "") {
    header.Authorization = "Bearer " + app.globalData.token;
  }
  if(params.isNew){
    wx.uploadFile({
      filePath: params.url,
      name: params.name,
      url: url,
      header: header,
      formData: params.formData,
      success: function (res) {
        // wx.hideLoading();
        if (res.data) {
          /** start 根据需求 接口的返回状态码进行处理 */
          if (res.statusCode == 200) {
            if (res.data.status == "401") {
            } else {
              onSuccess(JSON.parse(res.data)); //request success
            }
          } else {
            onFailed(res.data.message); //request failed
          }
          /** end 处理结束*/
        }
      },
      fail: function (error) {
        onFailed(error); //failure for other reasons
      },
      complete(){
        wx.hideLoading()
      }
    })
  }else{
    wx.hideLoading()
    onSuccess({code:0,data:params.url})
    return
    wx.downloadFile({
      url: params.url, //仅为示例，并非真实的资源
      success(res1) {
        let params1 = res1;
        wx.uploadFile({
          filePath: params1.tempFilePath,
          name: params.name,
          url: url,
          header: header,
          formData: params.formData,
          success: function (res) {
            // wx.hideLoading();
            if (res.data) {
              /** start 根据需求 接口的返回状态码进行处理 */
              if (res.statusCode == 200) {
                if (res.data.status == "401") {
                } else {
                  onSuccess(JSON.parse(res.data)); //request success
                }
              } else {
                onFailed(res.data.message); //request failed
              }
              /** end 处理结束*/
            }
          },
          fail: function (error) {
            onFailed(error); //failure for other reasons
          }
        })
      }
    })
  }
  return

  wx.request({
    url: url,
    data: dealParams(params),
    method: method,
    header: header,
    success: function (res) {
      // wx.hideLoading();
      if (res.data) {
        /** start 根据需求 接口的返回状态码进行处理 */
        if (res.statusCode == 200) {
          if (res.data.status == "401") {
            app.globalData.userInfo = null;
            app.globalData.token = "";
            app.globalData.openId = "",
              wx.reLaunch({
                url: '/pages/index/index',
              })
            wx.showToast({
              title: '登录过期,正在重新登录',
              icon: "none",
            })

          } else {
            onSuccess(res.data); //request success
          }
        } else {
          onFailed(res.data.message); //request failed
        }
        /** end 处理结束*/
      }
    },
    fail: function (error) {
      onFailed(""); //failure for other reasons
    }
  })
}

/**
 * function: 根据需求处理请求参数：添加固定参数配置等
 * @params 请求参数
 */
function dealParams(params) {
  return params;
}

// function redLogin() {
//   var that = this;
//   wx.login({
//     success(res) {
//       if (res.code) {
//         post(config.url + "/user/weChatLogin", {
//             code: res.code
//           },
//           function (res) {
//             if (res.status == 201) {
//               app.globalData.openId = res.data;
//             } else if (res.status == 200) {
//               app.globalData.userInfo = res.data;
//               app.globalData.token = res.data.token;


//             }

//           },
//           function (err) {

//           })
//       } else {

//       }
//     }
//   })


// }

// 1.通过module.exports方式提供给外部调用
module.exports = {
  upRequest: post,
}